package com.army.stock.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.army.stock.dao.ArmyYearplanDao;
import com.army.stock.domain.ArmyYearplan;
import com.army.stock.domain.ArmyYearplanItem;
import com.army.stock.service.ArmyYearplanService;
import com.javalife.common.domain.page.PaginatedArrayList;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhangyong on 15/1/7.
 * 年度计划
 */
public class ArmyYearplanServiceImpl implements ArmyYearplanService {

    private final static Logger logger = LoggerFactory.getLogger(ArmyYearplanServiceImpl.class);

    private ArmyYearplanDao armyYearplanDao;

    public void setArmyYearplanDao(ArmyYearplanDao armyYearplanDao) {
        this.armyYearplanDao = armyYearplanDao;
    }

    public PaginatedArrayList getArmyYearplanPageInfo(int currentPage, int pageSize, Map<String, Object> map) {
        int startNum = (currentPage - 1) * pageSize;
        map.put("start", startNum);
        map.put("pageSize", pageSize);
        Integer count = armyYearplanDao.getCountArmyYearplans(map);
        List<ArmyYearplan> Yearplans = armyYearplanDao.getArmyYearplans(map);
        PaginatedArrayList<ArmyYearplan> paginatedArrayList = new PaginatedArrayList<ArmyYearplan>(currentPage, pageSize);
        paginatedArrayList.setTotalItem(count);
        paginatedArrayList.addAll(Yearplans);
        return paginatedArrayList;
    }

    public PaginatedArrayList getArmyYearplanPageInfoForFinance(int currentPage, int pageSize, Map<String, Object> map) {
        int startNum = (currentPage - 1) * pageSize;
        map.put("start", startNum);
        map.put("pageSize", pageSize);
        Integer count = armyYearplanDao.getCountArmyYearplansFinance(map);
        List yearplans = armyYearplanDao.getArmyYearplansMapList(map);
        PaginatedArrayList<ArmyYearplan> paginatedArrayList = new PaginatedArrayList<ArmyYearplan>(currentPage, pageSize);
        paginatedArrayList.setTotalItem(count);
        paginatedArrayList.addAll(yearplans);
        return paginatedArrayList;
    }

    public PaginatedArrayList getArmyYearplanItemsPageInfo(int currentPage, int pageSize, Map<String, Object> map) {
        int startNum = (currentPage - 1) * pageSize;
        map.put("start", startNum);
        map.put("pageSize", pageSize);
        Integer count = armyYearplanDao.getCountArmyYearplanItems(map);
        List<Map> yearplanItems = armyYearplanDao.getArmyYearplanItems(map);
        PaginatedArrayList paginatedArrayList = new PaginatedArrayList(currentPage, pageSize);
        paginatedArrayList.setTotalItem(count);
        paginatedArrayList.addAll(yearplanItems);
        return paginatedArrayList;
    }

    @Override
    public List getAllArmyYearPlanItems(Long id) {
        return armyYearplanDao.getAllArmyYearPlanItems(id);
    }

    public void addArmyYearPlan(ArmyYearplan armyYearplan) {
        //暂时没有实现
    }

    @Override
    public synchronized Map<String, Object> addArmyYearPlan(JSONObject jsonObject) {
        Map<String, Object> returnMap = new HashMap<String, Object>();

        String yearplanName = jsonObject.getString("yearplanName");
        String departmentId = jsonObject.getString("departmentId");
        String priceSum = jsonObject.getString("priceSum");
        String amountLimit = jsonObject.getString("amountLimit");
        String year = jsonObject.getString("year");

        //查看是否已经存在
        Map map = new HashMap();
        map.put("year", year);
        map.put("departmentId", departmentId);
        Integer count = armyYearplanDao.getCountArmyYearplans(map);
        if (count > 0) {
            returnMap.put("code", "1");
            return returnMap;
        }

        ArmyYearplan armyYearplan = new ArmyYearplan();
        armyYearplan.setYearplanName(yearplanName);
        armyYearplan.setAmountLimit(new BigDecimal(amountLimit));
        armyYearplan.setDepartmentId(Long.valueOf(departmentId));
        // 0 :初始状态 1： 已提交 2 ：已审核成功 3：已拒绝
        armyYearplan.setStatus(0);
        armyYearplan.setPriceSum(new BigDecimal(priceSum));
        armyYearplan.setYear(year);
        armyYearplanDao.insertArmyYearplan(armyYearplan);
        JSONArray jsonArray = jsonObject.getJSONArray("items");
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject item = jsonArray.getJSONObject(i);
            ArmyYearplanItem armyYearplanItem = new ArmyYearplanItem();
            armyYearplanItem.setYearPlanId(armyYearplan.getId());
            armyYearplanItem.setGoodId(item.getLongValue("goodId"));
            armyYearplanItem.setGoodNum(Integer.valueOf(item.getIntValue("goodnum")));
            armyYearplanItem.setGoodPrice(item.getBigDecimal("goodPrice"));
            armyYearplanItem.setSubtotal(
                    new BigDecimal(armyYearplanItem.getGoodNum()).
                            multiply(armyYearplanItem.getGoodPrice()));
            armyYearplanDao.insertArmyYearplanItem(armyYearplanItem);
        }

        returnMap.put("code", "0");
        return returnMap;
    }

    public synchronized void updateArmyYearPlan(JSONObject jsonObject) {
        String departmentId = jsonObject.getString("departmentId");
        String priceSum = jsonObject.getString("priceSum");
        String amountLimit = jsonObject.getString("amountLimit");
        String yearPlanId = jsonObject.getString("yearPlanId");
        ArmyYearplan armyYearplan = new ArmyYearplan();
        armyYearplan.setId(Long.valueOf(yearPlanId));
        armyYearplan.setAmountLimit(new BigDecimal(amountLimit));
        armyYearplan.setPriceSum(new BigDecimal(priceSum));
        //修改数据
        armyYearplanDao.updateArmyYearplan(armyYearplan);
        //删除原有记录
        armyYearplanDao.delArmyYearplanItemsByYearPlanId(Long.valueOf(yearPlanId));
        //重新插入新的数据
        JSONArray jsonArray = jsonObject.getJSONArray("items");
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject item = jsonArray.getJSONObject(i);
            ArmyYearplanItem armyYearplanItem = new ArmyYearplanItem();
            armyYearplanItem.setYearPlanId(armyYearplan.getId());
            armyYearplanItem.setGoodId(item.getLongValue("goodId"));
            armyYearplanItem.setGoodNum(Integer.valueOf(item.getIntValue("goodnum")));
            armyYearplanItem.setGoodPrice(item.getBigDecimal("goodPrice"));
            armyYearplanItem.setSubtotal(
                    new BigDecimal(armyYearplanItem.getGoodNum()).
                            multiply(armyYearplanItem.getGoodPrice()));
            armyYearplanDao.insertArmyYearplanItem(armyYearplanItem);
        }
    }

    public  ArmyYearplan getArmyYearplanById(Long id) {
        return armyYearplanDao.getArmyYearplanById(id);
    }

    public synchronized void applyArmyYearPlan(Long yearPlanId) {
        ArmyYearplan armyYearplan = new ArmyYearplan();
        armyYearplan.setId(yearPlanId);
        armyYearplan.setStatus(1);
        armyYearplanDao.updateArmyYearplan(armyYearplan);
    }

    public synchronized void reviewArmyYearPlan(ArmyYearplan armyYearplan) {
        armyYearplanDao.updateArmyYearplan(armyYearplan);
    }

    public List<ArmyYearplan> getArmyYearPlanByDepartmentId(Long departmentId) {
        return armyYearplanDao.getArmyYearPlanByDepartmentId(departmentId);
    }

    public ArmyYearplan getArmyYearPlanByDepartmentIdAndYear(Long departmentId, Integer year) {
        Map map = new HashMap();
        map.put("departmentId", departmentId);
        map.put("year", year);
        List<ArmyYearplan> armyYearplanList = armyYearplanDao.getArmyYearplans(map);
        if (CollectionUtils.isEmpty(armyYearplanList)) {
            return null;
        }
        return armyYearplanList.get(0);
    }

}

